package leetcode.editor.cn;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: yangwk
 * @date: 2023/6/7
 * @description:
 **/
public class P120 {
    public static void main(String[] args) {
        Integer[][] arr = new Integer[][]{{2}, {3, 4}, {6, 5, 7}, {4, 1, 8, 3}};
        List<Integer[]> integers = Arrays.asList(arr);
        List<List<Integer>> triangle = new ArrayList<>();
        for (Integer[] integer : integers) {
            ArrayList<Integer> objects = new ArrayList<>();
            objects.addAll(Arrays.asList(integer));
            triangle.add(objects);
        }
        System.out.println(minimumTotal(triangle));

    }

    public static int minimumTotal(List<List<Integer>> triangle) {
        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < triangle.size(); i++) {
            List<Integer> row = triangle.get(i);
            List<Integer> val = new ArrayList<>();
            for (int j = 0; j < row.size(); j++) {
                if (j == 0) {
                    val.add(result.get(j) + row.get(j));
                } else {
                    if (j < result.size()) {
                        int min = Math.min(result.get(j) + row.get(j), row.get(j) + result.get(j - 1));
                        val.add(min);
                    } else {
                        val.add(row.get(j) + result.get(j - 1));
                    }
                }
            }
            result = val;
        }
        Integer min = result.stream().min(Integer::compare).get();
        return min;
    }
}
